﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HomeDMSDomain;
using System.IO;
using System.Windows.Threading;
using System.Linq;
using System.Data.Linq;

using System.Windows.Media.Imaging;

namespace bind_n_find
{
    /// <summary>
    /// Interaction logic for view_ctrl.xaml
    /// </summary>
    public partial class view_ctrl : UserControl, System.IDisposable
    {
        #region private fields

        private BindnFindObjectsDataContext _currentContext;
        private dlg_mode_e _dlg_mode = dlg_mode_e.view_t;
        private delegate void SimpleDelegate();
        private Document _doc;

        #endregion //private fields

        #region public properties
        /// <summary>
        /// Dialog mode
        /// </summary>
        public dlg_mode_e dlg_mode
        {
            get { return _dlg_mode; }
            set { _dlg_mode = value; }
        }

        #endregion //public properties

        #region construction and initialization
        /// <summary>
        /// Constructor
        /// </summary>
        public view_ctrl()
        {
            InitializeComponent();
            _init();
        }

        /// <summary>
        /// Initialization
        /// </summary>
        private void _init()
        {
            //is fired when bitmap list is updated
            new_edit_dlg.bi_list_updated += new EventHandler(new_edit_dlg_bi_list_updated);
            //is fired when image is changed in navigator control
            navigator1.image_changed += new navigation_toolbar_ctrl.image_changed_event_handler(navigator1_image_changed);
        }

        #endregion //construction and initialization

        #region event handlers
        /// <summary>
        /// Image is changed in navigator control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void navigator1_image_changed(object sender, image_changed_event_args e)
        {
            try
            {
                image1.Source = e.image;
                if ((_doc.Pages.Count - 1) >= navigator1.image_index && navigator1.image_index >= 0)
                {
                    //change image name when image is changed
                    textBlock5.Text = _doc.Pages[navigator1.image_index].Name;
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'view_ctrl.navigator1_image_changed': {0}", ex.Message));

            }

        }

        /// <summary>
        /// Bitmap list is updated
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void new_edit_dlg_bi_list_updated(object sender, EventArgs e)
        {
            try
            {
                //update imagelist of navigator control
                _update_image();
                _loading_label.Visibility = Visibility.Hidden;
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'view_ctrl.new_edit_dlg_bi_list_updated': {0}", ex.Message));

            }
        }
        /// <summary>
        /// View control is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void view_ctrl1_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                _load_data();
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'view_ctrl.view_ctrl1_Loaded': {0}", ex.Message));

            }


        }

        /// <summary>
        /// 'Close' button is clicked (close parent window)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //get parent window
            Window w = Window.GetWindow(this);
            //close it
            if (w != null) w.Close();
        }

        #endregion //event handlers

        #region private members

        /// <summary>
        /// Update imagelist of navigator control
        /// </summary>
        private void _update_image()
        {
            try
            {
                List<image_text_item_template> bi_list = (List<image_text_item_template>)Application.Current.Properties["BitmapImagesList"];
                if (bi_list != null)
                {
                    if (bi_list.Count >= 1)
                    {
                        navigator1.list = bi_list;
                    }
                }
                else
                {
                    _loading_label.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'view_ctrl._update_image': {0}", ex.Message));

            }

        }

    
        /// <summary>
        /// Load data 
        /// </summary>
        private void _load_data()
        {
            try
            {
                if (_dlg_mode == dlg_mode_e.new_t)
                    return;
                _currentContext = BindnFindObjectsDataContext.Instance;
                if (_currentContext == null)
                    return;
                _doc = _currentContext.Documents.Single<Document>(d => d.document_id == Properties.Settings.Default.CurrentDocumentID);
                if (_doc == null)
                    return;
                textBlock1.Text = _doc.EnteredOn.ToString();
                textBlock2.Text = _doc.Name;
                textBlock3.Text = _doc.Description;
                textBlock4.Text = _doc.Keywords;
                _update_image();
            }
            catch (Exception ex)
            {
                BNFTrace.DoTrace(TraceTypes.Error, string.Format("Exception in 'view_ctrl._load_data': {0}", ex.Message));

            }
          

        }

        #endregion //private members


        public void Dispose()
        {

        }
        
    }
}
